<template>
  <div class="data_container">
    <div class="data_main">
      <!-- 主体头部 -->
      <div class="top_area">
        <i class="el-icon-arrow-left"></i>
        <span class="return_operation" @click="returnOperation">返回</span>
        <span class="leftVertical_ine"></span>
        <span class="data_name">导入模型</span>
        <span style="display: inline-block; float: right">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </span>
      </div>
      <!-- 主体区域 -->
      <div class="form">
        <el-form
          :model="ruleForm"
          :label-position="labelPosition"
          :rules="rules"
          ref='ruleForm'
          label-width='11rem'
        >
          <template v-for="(item, index) in formData">
            <el-form-item
              :label="item.text"
              v-if="item.type === 0"
              :prop="item.id"
              :key="index"
            >
              <el-input
                v-model="ruleForm[item.id]"
                :placeHolder="item.placeHolder"
              ></el-input>
            </el-form-item>
            <el-form-item
              :label="item.text"
              v-else-if="item.type === 'drop'"
              :prop="item.id"
              :key="index"
            >
              <el-select
                v-model="ruleForm[item.id]"
                :placeholder="item.placeHolder"
              >
                <el-option
                  v-for="(item, index) in (item.id === 'model_type' ? datas : subDatas)"
                  :key="index"
                  :value="item.text"
                  :label="item.text"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              :label="item.text"
              v-else-if="item.type === 'description'"
              :prop="item.id"
              :key="index"
            >
              <el-input
                type='textarea'
                v-model="ruleForm[item.id]"
                :placeHolder="item.placeHolder"
                :autosize="{ minRows: 4, maxRows: 6 }"
              ></el-input>
            </el-form-item>
          </template>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import infoComponentVue from '../../../../components/infoComponent/infoComponent.vue';
import data from '../../../../components/createData.js';
import datas from '../../../../components/engineData.js';
export default {
  components: {
    infoComponentVue,
  },
  data() {
    return {
      labelPosition: 'left',
      ruleForm: {
        'model_name': '',
        'model_version': '',
        'description': '',
        'model_type': '',
        'runtime': '',
        'source_location': '',
      },
      rules: {},
      datas,
      subDatas: [],
      formData: data.modelOptions,
    };
  },
  created() {;
    this.formData.forEach((element, index) => {
      this.ruleForm[element.id] = '';
      this.rules[element.id] = element.rule;
    });
  },
  watch: {
    'ruleForm.model_type': {
      handler(v) {
        let tem = this.datas.filter(item => {
          return item.text === v
        })
        this.subDatas = tem[0].children
      }
    }
  },
  methods: {
    // 返回上一级
    returnOperation() {
      this.$router.push({ path: '/Model' });
    },
    // 创建
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$url.post('data/train/model', this.ruleForm).then((res) => {
            if (res.status === 200) {
              this.$message.success('创建成功');
              this.$router.push({ path: '/Model' });
            } else {
              this.$message.error('创建失败');
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.data_container {
  // 主体区域样式
  .data_main {
    width: 1520px;
    height: 710px !important;
    .top_area {
      margin-bottom: 40px;
    }
    .form {
      width: 760px !important;
      padding: 10px 740px 10px 40px;
      border: 1px solid #e6e6e6;
      .el-form-item {
        margin-top: 20px;
      }
      .el-select {
        width: 585px;
      }
    }
  }
}
</style>
